import Vue from 'vue'
import VueRouter from 'vue-router'
// import login from '../components/login.vue'
// import home from '../components/home.vue'
// import welcome from '../components/Welcome.vue'
// import Users from '../components/user/Users.vue'
// import rights from '../components/power/Rights.vue'
// import roles from '../components/power/Roles.vue'
// import cate from '../components/goods/cate.vue'
// import Params from '../components/goods/params.vue'
// import goods from '../components/goods/list.vue'
// import Add from '../components/goods/Add.vue'
// import order from '../components/order/order.vue'
// import report from '../components/report/report.vue'

// import Login from './components/Login.vue'
const login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/login.vue')
// import Home from './components/Home.vue'
const home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/home.vue')
// import Welcome from './components/Welcome.vue'
const welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')

// import Users from './components/user/Users.vue'
const Users = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/user/Users.vue')
// import Rights from './components/power/Rights.vue'
const rights = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Rights.vue')
// import Roles from './components/power/Roles.vue'
const roles = () => import(/* webpackChunkName: "Users_Rights_Roles" */ '../components/power/Roles.vue')

// import Cate from './components/goods/Cate.vue'
const cate = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/cate.vue')
// import Params from './components/goods/Params.vue'
const Params = () => import(/* webpackChunkName: "Cate_Params" */ '../components/goods/params.vue')

// import GoodsList from './components/goods/List.vue'
const goods = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/list.vue')
// import Add from './components/goods/Add.vue'
const Add = () => import(/* webpackChunkName: "GoodsList_Add" */ '../components/goods/Add.vue')

// import Order from './components/order/Order.vue'
const order = () => import(/* webpackChunkName: "Order_Report" */ '../components/order/order.vue')
// import Report from './components/report/Report.vue'
const report = () => import(/* webpackChunkName: "Order_Report" */ '../components/report/report.vue')


 Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  { path: '/home',
   component: home ,
   redirect:'/welcome',
   children:[
     {path:'/welcome',component:welcome},
     {path:'/users',component:Users},
     {path:'/rights',component:rights},
     {path:'/roles',component:roles},
     {path:'/categories',component:cate},
     {path:'/params',component:Params},
     {path:'/goods',component:goods},
     {path:'/goods/Add',component:Add},
     {path:'/orders',component:order},
     {path:'/reports',component:report},
    ]
  }
]

const router = new VueRouter({
  routes
})
//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  //to 将要访问的路径
  //from 代表从那个路径跳转而来
  //next 是一个函数，表示放行
  // next() 放行  next('/login') 强制跳转
  if(to.path === '/login') return next()
  //获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login') //如果token为空强制跳转回登录页
  next()
})
export default router
